package net.hn.hnms.biz.shipped.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.hn.hnms.biz.shipped.Listener.TransportDeliveryInfoListener;
import net.hn.hnms.biz.shipped.Listener.TransportDeliveryStatisticsListener;
import net.hn.hnms.biz.shipped.domain.TransportDeliveryInfo;
import net.hn.hnms.biz.shipped.domain.TransportDeliveryStatistics;
import net.hn.hnms.biz.shipped.mapper.TransportDeliveryInfoMapper;
import net.hn.hnms.biz.shipped.mapper.TransportDeliveryStatisticsMapper;
import net.hn.hnms.biz.shipped.request.TransportDeliveryStatisticsReq;
import net.hn.hnms.biz.shipped.service.ITransportDeliveryStatisticsService;
import net.hn.hnms.biz.shipped.vo.TransportDeliveryStatisticsVO;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wangjr
 * 发运统计表服务层实现
 * @date 2022-08-30
 */

@Service
@DS("master")
public class TransportDeliveryStatisticsServiceImpl  extends BaseService<TransportDeliveryStatistics, TransportDeliveryStatisticsVO> implements ITransportDeliveryStatisticsService {

    @Autowired
    private TransportDeliveryStatisticsMapper transportDeliveryStatisticsMapper;

    @Autowired
    private TransportDeliveryInfoMapper transportDeliveryInfoMapper;

    @Override
    public ResponsePage<TransportDeliveryStatisticsVO> selectPageList(TransportDeliveryStatisticsReq req, PageQuery pageQuery) {
        IPage<TransportDeliveryStatisticsVO> result = transportDeliveryStatisticsMapper.selectVoPage(pageQuery.build(), buildQuery(req));
        return new ResponsePage(result.getRecords(), result.getTotal());
    }

    private LambdaQueryWrapper<TransportDeliveryStatistics> buildQuery(TransportDeliveryStatisticsReq req){
        LambdaQueryWrapper<TransportDeliveryStatistics> query = Wrappers.lambdaQuery();
        query.eq(StrUtil.isNotBlank(req.getMineCode()),TransportDeliveryStatistics::getMineCode,req.getMineCode());
        query.eq(StrUtil.isNotBlank(req.getCoalName()),TransportDeliveryStatistics::getCoalName,req.getCoalName());
        query.eq(StrUtil.isNotBlank(req.getBusinessType()), TransportDeliveryStatistics::getType,req.getBusinessType());
        query.eq(StrUtil.isNotBlank(req.getDeptName()),TransportDeliveryStatistics::getDeptName,req.getDeptName());
        query.eq(StrUtil.isNotBlank(req.getCustName()),TransportDeliveryStatistics::getCustName,req.getCustName()).orderByDesc(TransportDeliveryStatistics::getReceiveGrossNum);

        if(req.getBeginDate() != null) query.apply(" create_time >= to_timestamp('"+ DateUtil.formatDate(req.getBeginDate())+"','yyyy-mm-dd hh24:mi:ss') ");
        if(req.getEndDate() != null) query.apply(" create_time <= to_timestamp('"+DateUtil.formatDate(req.getEndDate())+"','yyyy-mm-dd hh24:mi:ss') ");
        return query;
    }

    @Override
    public List<Map<String, Object>> dictList( String mineCode) {
        List<Map<String, Object>> result = new ArrayList<>();

        LambdaQueryWrapper<TransportDeliveryStatistics> queryCoalName = new LambdaQueryWrapper<>();
        queryCoalName.eq(StrUtil.isNotBlank(mineCode),TransportDeliveryStatistics::getMineCode,mineCode);
        queryCoalName.groupBy(TransportDeliveryStatistics::getCoalName);
        queryCoalName.select(TransportDeliveryStatistics::getCoalName);
        List<Map<String, Object>> coalNames = transportDeliveryStatisticsMapper.selectMaps(queryCoalName);
        if(CollUtil.isNotEmpty(coalNames)) result.addAll(coalNames);

        LambdaQueryWrapper<TransportDeliveryStatistics> queryDept = new LambdaQueryWrapper<>();
        queryDept.eq(StrUtil.isNotBlank(mineCode),TransportDeliveryStatistics::getMineCode,mineCode);
        queryDept.groupBy(TransportDeliveryStatistics::getDeptName);
        queryDept.select(TransportDeliveryStatistics::getDeptName);
        List<Map<String, Object>> deptNames = transportDeliveryStatisticsMapper.selectMaps(queryDept);
        if(CollUtil.isNotEmpty(deptNames)) result.addAll(deptNames);
        return result;
    }

    @Override
    public void shippingStatisticsImport(MultipartFile file) {
        String sufix = StrUtil.subAfter(file.getOriginalFilename(), ".", true);
        if (!"xls".equals(sufix) && !"xlsx".equals(sufix))
            throw new HuaNengException(Constants.FAIL, "文件格式仅支持xls或xlsx");

        try {
            EasyExcel.read(file.getInputStream(), TransportDeliveryStatistics.class, new TransportDeliveryStatisticsListener(transportDeliveryStatisticsMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new HuaNengException(Constants.FAIL, "内容格式错误！");
        }
    }

    @Override
    public void shippingInfoImport(MultipartFile file) {
        String sufix = StrUtil.subAfter(file.getOriginalFilename(), ".", true);
        if (!"xls".equals(sufix) && !"xlsx".equals(sufix))
            throw new HuaNengException(Constants.FAIL, "文件格式仅支持xls或xlsx");

        try {
            EasyExcel.read(file.getInputStream(), TransportDeliveryInfo.class, new TransportDeliveryInfoListener(transportDeliveryInfoMapper)).sheet().doRead();
        } catch (IOException e) {
            throw new HuaNengException(Constants.FAIL, "内容格式错误！");
        }
    }
}
